﻿Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb
Imports System.IO

Public Class frmMain
    Dim cn As New SqlConnection
    Private _Batch As Integer = 0
    Private _school As String = ""
    Private _csf As Integer = 0
    Private _atag As String = "Lecture"
    Private _duration As Integer = 1
    Private _section As Integer = 0
    Private _room As Integer = 0
    Private _TTIdCopy = 0
    Private _sectionCopy = 0
    Private _TTDayCopy = 0
    Private _TTPeriodCopy = 0
    Private _currrow As Integer = 0
    Private _currFacId As Integer = 0
    Private beforeDel As Integer = 0
    Private TTFillBothColor(7, 9) As Integer
    Private TTFillFacColor(7, 9) As Integer
    Private TTFillRoomColor(7, 9) As Integer
    Private _colorMode As Boolean = True

    Dim disession As SessionData
    Sub TTAdd(ByVal day As Integer, ByVal period As Integer, ByVal value As String)
        TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).Text = TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).Text.Trim & vbCrLf & value
    End Sub
    Sub TTFacAdd(ByVal day As Integer, ByVal period As Integer, ByVal sectionid As Integer)
        'Dim m = 0
        'For i = 0 To 6
        '    For j = 0 To 8
        '        m = Math.Abs(TTFillFacColor.GetValue(i, j) + TTFillRoomColor.GetValue(i, j))
        '        TTFillBothColor.SetValue(m, i, j)
        '    Next
        'Next
        If Not (TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightGreen) Then
            If sectionid = _section Then
                'If TTFillRoomColor.GetValue(period - 1, day - 1) = 1 Then
                '    TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightGreen
                'Else
                '    TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightBlue
                'End If
                TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightBlue
            Else
                'If TTFillRoomColor.GetValue(period - 1, day - 1) = 1 Then
                '    TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightGreen
                'Else
                '    TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightPink
                'End If
                TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightPink

            End If
        End If
    End Sub
    Sub TTRoomAdd(ByVal day As Integer, ByVal period As Integer)
        TableLayoutPanel1.GetControlFromPosition(period - 1, day - 1).BackColor = Color.LightGreen
    End Sub
    Sub LoadRoomTT(ByVal RoomId As Integer)
        Array.Clear(TTFillRoomColor, 0, 63)

        For i = 0 To TableLayoutPanel1.Controls.Count - 1
            TableLayoutPanel1.Controls.Item(i).BackColor = Color.White

        Next

        ' Dim cn As New SqlConnection
        Dim sQry = "SELECT TT_Day, TT_Period FROM [V_TimeTable_2013] WHERE (TimeTableId = " & My.Settings.TTid & ") and (Room_id=" & RoomId & ")"
        Dim cmd As New SqlCommand(sQry)
        Dim ad As SqlDataReader
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        cmd.Connection = cn
        ad = cmd.ExecuteReader()
        Do While ad.Read
            TTRoomAdd(ad.Item("TT_Day"), ad.Item("TT_Period"))
            TTFillRoomColor.SetValue(1, ad.Item("TT_Day"), ad.Item("TT_Period"))
        Loop
        If cn.State = ConnectionState.Open Then cn.Close()
    End Sub

    Sub LoadFacTT(ByVal FacultyId As Integer)
        FacultyId = _currFacId

        For i = 0 To TableLayoutPanel1.Controls.Count - 1
            If Not _colorMode Then
                TableLayoutPanel1.Controls.Item(i).BackColor = Color.White
            Else
                If Not (TableLayoutPanel1.Controls.Item(i).BackColor = Color.LightGreen) Then
                    TableLayoutPanel1.Controls.Item(i).BackColor = Color.White

                End If
            End If

        Next


        'Dim cn As New SqlConnection
        Dim sQry = "SELECT TT_Day, TT_Period, section_id FROM [V_TimeTable_2013] WHERE (TimeTableId = " & My.Settings.TTid & ") and (id=" & FacultyId & ")"
        Dim cmd As New SqlCommand(sQry)
        'SqlCommand cmd = new SqlCommand(sQry, GlobalCnx); 
        Dim ad As SqlDataReader
        If cn.State = ConnectionState.Open Then cn.Close()
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        cmd.Connection = cn
        ad = cmd.ExecuteReader()
        Do While ad.Read
            TTFacAdd(ad.Item("TT_Day"), ad.Item("TT_Period"), ad.Item("Section_Id"))
            TTFillFacColor.SetValue(1, ad.Item("TT_Day"), ad.Item("TT_Period"))
        Loop
        If cn.State = ConnectionState.Open Then cn.Close()
    End Sub

    Sub LoadTT(ByVal SectionId As Integer)
        For i = 0 To TableLayoutPanel1.Controls.Count - 1
            TableLayoutPanel1.Controls.Item(i).Text = ""
        Next

        '  Dim cn As New SqlConnection
        'Dim sQry = "SELECT TimeTableId, Section_Id, TT_Day, TT_Period, CSF_Id, Room_Id, Batch_Id, Id, Name, Abr, Subject_Code, Subject, IsLab, Section_Name, Semester, Session_Id, Room, Group_Id, Dept, HOD, Section_Set  FROM [V_TimeTable_2013] WHERE (TimeTableId = " & My.Settings.TTid & ") and (Section_id=" & SectionId & ")"
        Dim sQry = "SELECT TimeTableId, Section_Id, TT_Day, TT_Period, CSF_Id, Room_Id, Batch_Id, Id, Name, Abr, Subject_Code, Subject, IsLab, Section_Name, Semester, Room, Group_Id  FROM [V_TimeTable_2013] WHERE (TimeTableId = " & My.Settings.TTid & ") and (Section_id=" & SectionId & ")"
        Dim cmd As New SqlCommand(sQry)
        'SqlCommand cmd = new SqlCommand(sQry, GlobalCnx); 
        Dim ad As SqlDataReader
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        cmd.Connection = cn
        ad = cmd.ExecuteReader()
        Do While ad.Read
            If ad.Item("Batch_Id") <> 0 Then
                TTAdd(ad.Item("TT_Day"), ad.Item("TT_Period"), ad.Item("Subject_Code").ToString.Trim & " [" & ad.Item("ABR").ToString.Trim & "][" & ad.Item("rOOM").ToString.Trim & "][" & ad.Item("Batch_id").ToString.Trim & "]")
            Else
                TTAdd(ad.Item("TT_Day"), ad.Item("TT_Period"), ad.Item("Subject_Code").ToString.Trim & " [" & ad.Item("ABR").ToString.Trim & "][" & ad.Item("rOOM").ToString.Trim & "]")
            End If

        Loop

        If cn.State = ConnectionState.Open Then cn.Close()
        If cbSession.SelectedIndex = -1 Then Exit Sub
        disession = cbSession.SelectedItem
        'CSFUpdate(ToolStripComboBox2.ComboBox.SelectedValue, disession.Get_Id1)
        _section = cbSection.ComboBox.SelectedValue
    End Sub

    Sub LoadTTAt(ByVal day As Integer, ByVal period As Integer, ByVal SectionId As Integer)
        LoadTT(SectionId)
        ''Dim cn As New SqlConnection
        ''Dim sQry = "SELECT TimeTableId, Section_Id, TT_Day, TT_Period, CSF_Id, Room_Id, Batch_Id, Id, Name, Abr, Subject_Code, Subject, IsLab, Section_Name, Semester, Session_Id, Room, Group_Id, Dept, HOD, Section_Set  FROM [V_TimeTable_2013] WHERE (TimeTableId = " & My.Settings.TTid & ") and (TT_Day=" & day & ") and (TT_Period=" & period & ") and (Section_id=" & SectionId & ")"
        'Dim sQry = "SELECT TimeTableId, Section_Id, TT_Day, TT_Period, CSF_Id, Room_Id, Batch_Id, Id, Name, Abr, Subject_Code, Subject, IsLab, Section_Name, Semester, Room, Group_Id  FROM [V_TimeTable_2013] WHERE (TimeTableId = " & My.Settings.TTid & ") and (TT_Day=" & day & ") and (TT_Period=" & period & ") and (Section_id=" & SectionId & ")"
        'Dim cmd As New SqlCommand(sQry)
        ''SqlCommand cmd = new SqlCommand(sQry, GlobalCnx); 
        'Dim ad As SqlDataReader
        'cn.ConnectionString = My.Settings.eCollegeConnectionString
        'cn.Open()
        'cmd.Connection = cn
        'ad = cmd.ExecuteReader()
        'Do While ad.Read
        '    TTAdd(ad.Item("TT_Day"), ad.Item("TT_Period"), ad.Item("Subject_Code").ToString.Trim & " [" & ad.Item("ABR").ToString.Trim & "][" & ad.Item("rOOM").ToString.Trim & "]")
        'Loop
        'If cn.State = ConnectionState.Open Then cn.Close()
        'If cbSession.SelectedIndex = -1 Then Exit Sub
        'disession = cbSession.SelectedItem
        ''CSFUpdate(ToolStripComboBox2.ComboBox.SelectedValue, disession.Get_Id1)
        '_section = cbSection.ComboBox.SelectedValue

    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        LoginForm1.Close()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' frmCourseStructure.ShowDialog()

        'frmActivation.ShowDialog()
        'TODO: This line of code loads data into the 'ECollegeDataSet.Section' table. You can move, or remove it, as needed.
        'If My.Settings.ProgramFilter Then
        '    Me.SectionTableAdapter.FillBy(Me.ECollegeDataSet.Section, cbSession.ComboBox.SelectedValue)
        'Else
        '    Me.SectionTableAdapter.Fill(Me.ECollegeDataSet.Section)
        'End If
        My.Settings.ProgramFilter = False
        ' cbSession.ComboBox.DataSource = bsProgram
        'cbSession.ComboBox.DisplayMember = "name".Trim()
        'cbSession.ComboBox.ValueMember = "Id"
        If My.Settings.ProgramFilter Then
            Me.SectionTableAdapter.FillBy(Me.ECollegeDataSet.Section, cbSession.ComboBox.SelectedValue)

        Else
            Me.SectionTableAdapter.Fill(Me.ECollegeDataSet.Section)
            cbSession.Visible = False
        End If

        _Batch = 0
        'FillSessionCombo()
        'cbSection.ComboBox.DataSource = MSectionBindingSource
        cbSection.ComboBox.DataSource = bsSection
        cbSection.ComboBox.DisplayMember = "name".Trim()
        cbSection.ComboBox.ValueMember = "Id"

        Try
            _section = cbSection.ComboBox.SelectedValue
            CSFUpdate(_section)
            LoadTT(_section)
        Catch ex As Exception

        End Try


    End Sub
    '' ''Sub FillSessionCombo()
    '' ''    Dim sQry = "SELECT * From M_Session order by session_id DESC"
    '' ''    cn.ConnectionString = My.Settings.eCollegeConnectionString
    '' ''    cn.Open()
    '' ''    Dim cmd As New SqlCommand(sQry, cn)
    '' ''    Dim ad As New SqlDataAdapter(cmd)
    '' ''    Dim ds As New DataSet
    '' ''    ad.Fill(ds)

    '' ''    For i = 0 To ds.Tables(0).Rows.Count - 1
    '' ''        cbSession.Items.Add(New SessionData(ds.Tables(0).Rows(i).Item("Session_id"), ds.Tables(0).Rows(i).Item("Section_Set_Active"), ds.Tables(0).Rows(i).Item("Session_Name")))
    '' ''    Next
    '' ''    If cn.State = ConnectionState.Open Then cn.Close()
    '' ''End Sub

    Private Sub ToolStripComboBox2_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        'If cbSession.SelectedIndex = -1 Then Exit Sub
        ' disession = cbSession.SelectedItem

        'CSFUpdate(cbSection.SelectedItem, disession.Get_Id1)
        CSFUpdate(cbSection.SelectedItem)
        _section = cbSection.SelectedItem
        LoadTT(cbSection.SelectedItem)
    End Sub

    Private Sub TTCellClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click, TextBox2.Click, TextBox3.Click, TextBox4.Click, TextBox5.Click, TextBox6.Click, TextBox7.Click, TextBox8.Click, TextBox9.Click, TextBox10.Click, TextBox11.Click, TextBox12.Click, TextBox13.Click, TextBox14.Click, TextBox15.Click, TextBox16.Click, TextBox17.Click, TextBox18.Click, TextBox19.Click, TextBox20.Click, TextBox21.Click, TextBox22.Click, TextBox23.Click, TextBox24.Click, TextBox25.Click, TextBox26.Click, TextBox27.Click, TextBox28.Click, TextBox29.Click, TextBox30.Click, TextBox31.Click, TextBox32.Click, TextBox33.Click, TextBox34.Click, TextBox35.Click, TextBox36.Click, TextBox37.Click, TextBox38.Click, TextBox39.Click, TextBox40.Click, TextBox41.Click, TextBox42.Click, TextBox43.Click, TextBox44.Click, TextBox45.Click, TextBox46.Click, TextBox47.Click, TextBox48.Click, TextBox49.Click, TextBox50.Click, TextBox51.Click, TextBox52.Click, TextBox53.Click, TextBox54.Click, TextBox55.Click, TextBox56.Click
        RoomUpdate(sender)
    End Sub

    Sub RoomUpdate(ByVal sender As Object)
        Dim ts As New Timesheet

        Dim da = TableLayoutPanel1.GetRow(sender) + 1
        Dim pr = TableLayoutPanel1.GetColumn(sender) + 1
        CheckRoomSchool()
        If _school = "NONE" Then
            ts.RoomUpdate(da, pr, DataGridView1)
        Else
            ts.RoomUpdate(da, pr, _school, DataGridView1)
        End If

        'Dim sQry = ""

        'If chkLabs.Checked = True Then
        '    sQry = "SELECT room_id ,name  FROM M_Room where islab=1 and (roomtype<3) EXCEPT SELECT Room_Id,Room FROM V_TimeTable_2013 WHERE (TimeTableId = " & My.Settings.TTid & ") AND (TT_Day = " & da & ") AND (TT_Period = " & pr & ") order by Name"
        'Else
        '    sQry = "SELECT room_id ,name  FROM M_Room  where islab=0  and (roomtype<3)  EXCEPT SELECT Room_Id,Room FROM V_TimeTable_2013 WHERE (TimeTableId = " & My.Settings.TTid & ") AND (TT_Day = " & da & ") AND (TT_Period = " & pr & ") order by Name"
        'End If
        ''MsgBox(sQry)
        ''Dim cn As New SqlConnection
        'If cn.State = ConnectionState.Closed Then cn.ConnectionString = My.Settings.eCollegeConnectionString
        'cn.Open()
        ''Dim sQry = "SELECT TimeTableId, Section_Id, TT_Day, TT_Period, CSF_Id, Room_Id, Batch_Id, Id, Name, Abr, Subject_Code, Subject, IsLab, Section_Name, Semester, Session_Id, Room, Group_Id, Dept, HOD, Section_Set  FROM [V_TimeTable_2013] WHERE (TimeTableId = " & My.Settings.TTid & ") and (Section_id=" & SectionId & ")"
        'Dim cmd As New SqlCommand(sQry, cn)

        ''cmd.Parameters.Add(1)
        'Dim ad As New SqlDataAdapter(cmd)
        'Dim ds As New DataSet
        'ad.Fill(ds)
        'DataGridView1.DataSource = ds.Tables(0)
        'DataGridView1.Columns(0).Visible = False
        'If cn.State = ConnectionState.Open Then cn.Close()
    End Sub

    'Sub CSFUpdate(ByVal SectionId As Integer, ByVal SessionId As Integer)
    Sub CSFUpdate(ByVal SectionId As Integer)
        'SessionId = 10
        ' Dim sQry = "SELECT distinct csf_id,subject_code,abr,id,name,l_load,L,T,P from V_section_subject_Faculty_1 WHERE (section_id=" & SectionId & ") AND (session_id = " & SessionId & ")"
        'Dim sQry = "SELECT distinct csf_id,subject_code,abr,id,name,l_load,L,T,P from V_section_subject_Faculty_1 WHERE (section_id=" & SectionId & ")"

        Dim sQry = "SELECT distinct csf_id,subject_code,abr,Teacher_id as id,Teacher_name as name,l_load,L,T,P,LA,TA,PA,IsLab from CSF_View_with_Load WHERE (section_id=" & SectionId & ")"

        'Dim cn As New SqlConnection
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()

        Dim cmd As New SqlCommand(sQry, cn)
        'cmd.Parameters.Add(1)
        Dim ad As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        ad.Fill(ds)
        CSFList.DataSource = ds.Tables(0)

        If cn.State = ConnectionState.Open Then cn.Close()
    End Sub

    Private Sub EditTT(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick, TextBox2.DoubleClick, TextBox3.DoubleClick, TextBox4.DoubleClick, TextBox5.DoubleClick, TextBox6.DoubleClick, TextBox7.DoubleClick, TextBox8.DoubleClick, TextBox9.DoubleClick, TextBox10.DoubleClick, TextBox11.DoubleClick, TextBox12.DoubleClick, TextBox13.DoubleClick, TextBox14.DoubleClick, TextBox15.DoubleClick, TextBox16.DoubleClick, TextBox17.DoubleClick, TextBox18.DoubleClick, TextBox19.DoubleClick, TextBox20.DoubleClick, TextBox21.DoubleClick, TextBox22.DoubleClick, TextBox23.DoubleClick, TextBox24.DoubleClick, TextBox25.DoubleClick, TextBox26.DoubleClick, TextBox27.DoubleClick, TextBox28.DoubleClick, TextBox29.DoubleClick, TextBox30.DoubleClick, TextBox31.DoubleClick, TextBox32.DoubleClick, TextBox33.DoubleClick, TextBox34.DoubleClick, TextBox35.DoubleClick, TextBox36.DoubleClick, TextBox37.DoubleClick, TextBox38.DoubleClick, TextBox39.DoubleClick, TextBox40.DoubleClick, TextBox41.DoubleClick, TextBox42.DoubleClick, TextBox43.DoubleClick, TextBox44.DoubleClick, TextBox45.DoubleClick, TextBox46.DoubleClick, TextBox47.DoubleClick, TextBox48.DoubleClick, TextBox49.DoubleClick, TextBox50.DoubleClick, TextBox51.DoubleClick, TextBox52.DoubleClick, TextBox53.DoubleClick, TextBox54.DoubleClick, TextBox55.DoubleClick, TextBox56.DoubleClick
        Dim TTId As Integer = My.Settings.TTid
        Dim TTDay = TableLayoutPanel1.GetRow(sender) + 1
        Dim TTPeriod = TableLayoutPanel1.GetColumn(sender) + 1
        Dim Sectionid As Integer = _section
        Dim ATAG As String = _atag
        Dim CSF_ID As Integer = _csf
        Dim RooM_ID As Integer = _room
        Dim BATCHID As Integer = _Batch
        Dim ISDEL As Integer = 0
        Dim sQry As String = ""
        If CSF_ID = 0 Then Exit Sub
        If CheckBox1.Checked = True Then
            sQry = "INSERT INTO  M_Time_Table "
            sQry = sQry & "SELECT  " & TTId & " as TimeTableId ,   " & Sectionid & " as Section_Id,   " & TTDay & " as TT_Day,   " & TTPeriod & " as TT_Period, CSF_Id, Room_Id, Batch_Id, Section_Group_Id,MergeCSF,ActivityTag "
            sQry = sQry & "FROM M_Time_Table "
            sQry = sQry & "WHERE (TimeTableId = " & _TTIdCopy & ") And (Section_Id = " & _sectionCopy & ") And (TT_Day = " & _TTDayCopy & ") And (TT_Period = " & _TTPeriodCopy & ")"

        Else
            sQry = "Exec Timetablemanager @TTId=" & TTId & ",@TTDay=" & TTDay & ",@TTPeriod=" & TTPeriod & ",@Sectionid=" & Sectionid & ",@CSF_ID= " & CSF_ID & ", @RooM_ID=" & RooM_ID & ",@BATCHID=" & BATCHID & ",@ISDEL=" & ISDEL & ",@atag=" & ATAG & ",@duration=" & _duration
        End If
        'Dim cn As New SqlConnection
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(sQry, cn)

        cmd.ExecuteNonQuery()
        If cn.State = ConnectionState.Open Then cn.Close()
        'CSFList.Rows(_currrow).Cells(5).Value = CSFList.Rows(_currrow).Cells(5).Value + 1
        beforeDel = _currrow
        'CSFUpdate(cbSection.ComboBox.SelectedValue, disession.Get_Id1)
        CSFUpdate(cbSection.ComboBox.SelectedValue)
        CSFList.CurrentCell = CSFList.Rows(0).Cells(0)
        _Batch = 0
        UpdateBatchRadio()
        LoadTTAt(TTDay, TTPeriod, Sectionid)
        TTFacAdd(TTDay, TTPeriod, Sectionid)
        conflicts.ListFacConflict()
    End Sub
    Sub SetDefaultRoom()
        Dim TTId As Integer = My.Settings.TTid
        Dim sQry As String = ""
        sQry = "Update [M_time_table] "
        sQry = sQry & " SET Room_Id=" & _room
        sQry = sQry & " WHERE (TimeTableId = " & TTId & ") And (Section_Id = " & _section & ")  AND (Batch_Id = 0) "
        Dim cn As New SqlConnection
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(sQry, cn)
        cmd.ExecuteNonQuery()
        If cn.State = ConnectionState.Open Then cn.Close()
        LoadTT(_section)
    End Sub

    Sub DeleteTT(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown, TextBox5.KeyDown, TextBox6.KeyDown, TextBox7.KeyDown, TextBox8.KeyDown, TextBox9.KeyDown, TextBox10.KeyDown, TextBox11.KeyDown, TextBox12.KeyDown, TextBox13.KeyDown, TextBox14.KeyDown, TextBox15.KeyDown, TextBox16.KeyDown, TextBox17.KeyDown, TextBox18.KeyDown, TextBox19.KeyDown, TextBox20.KeyDown, TextBox21.KeyDown, TextBox22.KeyDown, TextBox23.KeyDown, TextBox24.KeyDown, TextBox25.KeyDown, TextBox26.KeyDown, TextBox27.KeyDown, TextBox28.KeyDown, TextBox29.KeyDown, TextBox30.KeyDown, TextBox31.KeyDown, TextBox32.KeyDown, TextBox33.KeyDown, TextBox34.KeyDown, TextBox35.KeyDown, TextBox36.KeyDown, TextBox37.KeyDown, TextBox38.KeyDown, TextBox39.KeyDown, TextBox40.KeyDown, TextBox41.KeyDown, TextBox42.KeyDown, TextBox43.KeyDown, TextBox44.KeyDown, TextBox45.KeyDown, TextBox46.KeyDown, TextBox47.KeyDown, TextBox48.KeyDown, TextBox49.KeyDown, TextBox50.KeyDown, TextBox51.KeyDown, TextBox52.KeyDown, TextBox53.KeyDown, TextBox54.KeyDown, TextBox55.KeyDown, TextBox56.KeyDown
        Dim result As Windows.Forms.DialogResult
        If e.KeyData = Keys.Delete Then
            If CheckBox1.Checked = True Then
                _TTIdCopy = My.Settings.TTid
                _sectionCopy = _section
                _TTDayCopy = TableLayoutPanel1.GetRow(sender) + 1
                _TTPeriodCopy = TableLayoutPanel1.GetColumn(sender) + 1
            Else

                If My.Settings.DeleteConfirm = True Then
                    result = MessageBox.Show("Are you sure to delete?", "Time Table 2010", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
                Else
                    result = Windows.Forms.DialogResult.Yes
                End If
                If result = Windows.Forms.DialogResult.Yes Then
                    Dim TTId As Integer = My.Settings.TTid

                    Dim TTDay = TableLayoutPanel1.GetRow(sender) + 1
                    Dim TTPeriod = TableLayoutPanel1.GetColumn(sender) + 1
                    TableLayoutPanel1.GetControlFromPosition(TTPeriod - 1, TTDay - 1).BackColor = Color.White
                    Dim Sectionid As Integer = _section
                    Dim CSF_ID As Integer = _csf
                    Dim RooM_ID As Integer = _room
                    Dim BATCHID As Integer = _Batch
                    Dim ISDEL As Integer = 1
                    Dim sQry = "Exec Timetablemanager @TTId=" & TTId & ",@TTDay=" & TTDay & ",@TTPeriod=" & TTPeriod & ",@Sectionid=" & Sectionid & ",@CSF_ID= " & CSF_ID & ", @RooM_ID=" & RooM_ID & ",@BATCHID=" & BATCHID & ",@ISDEL=" & ISDEL & ",@duration=" & _duration
                    'Dim cn As New SqlConnection
                    cn.ConnectionString = My.Settings.eCollegeConnectionString
                    cn.Open()
                    Dim cmd As New SqlCommand(sQry, cn)
                    cmd.ExecuteNonQuery()
                    If cn.State = ConnectionState.Open Then cn.Close()
                    LoadTT(_section)

                    beforeDel = _currrow
                    'CSFUpdate(cbSection.ComboBox.SelectedValue, disession.Get_Id1)
                    CSFUpdate(cbSection.ComboBox.SelectedValue)

                Else
                    Exit Sub
                End If
            End If
        End If
        CSFList.Refresh()
        Me.Refresh()
        conflicts.ListFacConflict()
    End Sub

    Private Sub CSFList_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CSFList.CellEndEdit
        Dim sQry As String = ""
        If (e.ColumnIndex = 6 Or e.ColumnIndex = 7 Or e.ColumnIndex = 8) Then
            Try
                sQry = "UPDATE Subject SET L=" & CSFList(6, e.RowIndex).Value & ", T=" & CSFList(7, e.RowIndex).Value & ", P=" & CSFList(8, e.RowIndex).Value & " WHERE (Code = '" & CSFList(1, e.RowIndex).Value & "')"
                'Dim cn As New SqlConnection
                cn.ConnectionString = My.Settings.eCollegeConnectionString
                cn.Open()
                ' MsgBox(sQry)
                Dim cmd As New SqlCommand(sQry, cn)
                cmd.ExecuteNonQuery()
                If cn.State = ConnectionState.Open Then cn.Close()
            Catch ex As Exception

            End Try

        End If
        If (e.ColumnIndex = 9 Or e.ColumnIndex = 10 Or e.ColumnIndex = 11) Then
            Try
                sQry = "UPDATE CSF SET LA=" & CSFList(9, e.RowIndex).Value & ", TA=" & CSFList(10, e.RowIndex).Value & ", PA=" & CSFList(11, e.RowIndex).Value & " WHERE (CSF_Id = '" & CSFList(0, e.RowIndex).Value & "')"
                'Dim cn As New SqlConnection
                cn.ConnectionString = My.Settings.eCollegeConnectionString
                cn.Open()
                ' MsgBox(sQry)
                Dim cmd As New SqlCommand(sQry, cn)
                cmd.ExecuteNonQuery()
                If cn.State = ConnectionState.Open Then cn.Close()
            Catch ex As Exception

            End Try

        End If
    End Sub

    Private Sub CSFList_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles CSFList.DataBindingComplete
        ColorCSFLTP()
    End Sub
    Private Sub CSFList_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CSFList.RowEnter
        ' _csf = CSFList.Item(0, e.RowIndex).Value
        _currrow = e.RowIndex
        LoadFacTT(CSFList.Item(3, e.RowIndex).Value)

    End Sub
    Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillByToolStripButton.Click
        Try
            ' Me.M_SessionTableAdapter.FillBy(Me.ECollegeDataSet1.M_Session)
            MessageBox.Show("Err:1")
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        LoadRoomTT(DataGridView1.Item(0, e.RowIndex).Value)
        _room = DataGridView1.Item(0, e.RowIndex).Value
        ToolStripLabel1.Text = "Current Room selected: " & DataGridView1.Item(1, e.RowIndex).Value
    End Sub

    Private Sub FillBy2ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            ' Me.M_SectionTableAdapter.FillBy2(Me.ECollegeDataSet.M_Section)
            MessageBox.Show("err:2")
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub BatchF_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BatchF.CheckedChanged
        _Batch = 0
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        _Batch = 1
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        _Batch = 2
    End Sub

    Private Sub PreferenceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreferenceToolStripMenuItem.Click
        TimeTableManager.Preference.ShowDialog()
    End Sub



    Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbSession.SelectedIndexChanged
        If cbSession.SelectedIndex = -1 Then Exit Sub
        'disession = cbSession.ComboBox.SelectedItem
        ' _currentSession = cbSession.ComboBox.SelectedValue 'disession.Get_Id2
        Try
            ' Me.M_SectionTableAdapter.FillBy1(Me.ECollegeDataSet.M_Section, CType(disession.Get_Id2, Integer))
            Me.SectionTableAdapter.FillBy(Me.ECollegeDataSet.Section, cbSession.ComboBox.SelectedValue)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub ToolStripComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbSection.SelectedIndexChanged
        'MsgBox(cbSession.SelectedIndex)
        'If cbSession.SelectedIndex = -1 Then Exit Sub
        'disession = cbSession.SelectedItem
        '        CSFUpdate(cbSection.ComboBox.SelectedValue, disession.Get_Id1)
        Try
            CSFUpdate(cbSection.ComboBox.SelectedValue)
            _section = cbSection.ComboBox.SelectedValue
            _currentSection = _section
            LoadTT(cbSection.ComboBox.SelectedValue)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub CSFList_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles CSFList.RowHeaderMouseClick
        _currrow = e.RowIndex
        _csf = CSFList.Item(0, e.RowIndex).Value
        Dim _atagTmp = CSFList.Item(12, e.RowIndex).Value
        If _atagTmp Then
            _atag = "Lab"
            _duration = CSFList.Item(8, e.RowIndex).Value
        Else
            _atag = "Lecture"
            _duration = 1
        End If
        tsLTPLabel.Text = _atag
        _currFacId = CSFList.Item(3, e.RowIndex).Value
        ToolStripTextBox1.Text = _csf
        'CSFList.RowHeadersDefaultCellStyle.BackColor = Color.D
        LoadFacTT(_currFacId)
    End Sub

    Private Sub CSFList_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles CSFList.RowHeaderMouseDoubleClick

        Try
            _currrow = e.RowIndex
            _currentcsf = CSFList.Item(0, e.RowIndex).Value
            Dim res = FRMcsfupdate.ShowDialog()
            If res = Windows.Forms.DialogResult.OK Then CSFUpdate(_section)
            If cbSession.SelectedIndex = -1 Then Exit Sub
            'disession = cbSession.SelectedItem
            'CSFUpdate(cbSection.ComboBox.SelectedValue, disession.Get_Id1)
            'CSFUpdate(cbSection.ComboBox.SelectedValue)
            '_section = cbSection.ComboBox.SelectedValue
            '_currentSection = _section
            'LoadTT(cbSection.ComboBox.SelectedValue)
        Catch ex As Exception

        End Try

    End Sub


    Private Sub ColorCSFLTP()
        Dim dr As DataGridViewRow
        For Each dr In CSFList.Rows
            Try
                Dim val1 As Integer = dr.Cells(5).Value
                Dim val3 As Integer = dr.Cells(8).Value
                Dim val2 As Integer = dr.Cells(9).Value + dr.Cells(10).Value + dr.Cells(11).Value
                If val1 = val2 Then dr.DefaultCellStyle.BackColor = Color.LightGreen
                If val1 > val2 Then dr.DefaultCellStyle.BackColor = Color.LightPink
                Dim font As Font = dr.DefaultCellStyle.Font
                If val3 > 0 Then dr.DefaultCellStyle.Font = New Font(font, System.Drawing.FontStyle.Bold)
            Catch ex As Exception
            End Try
        Next
    End Sub

    Private Sub CheckTeacherConflictsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTeacherConflictsToolStripMenuItem.Click
        conflicts.ListFacConflict()
        conflicts.Show()

    End Sub

    Private Sub CheckRoomConflictsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckRoomConflictsToolStripMenuItem.Click
        conflicts.ListRoomConflict()
        conflicts.Show()

    End Sub

    Private Sub SetDefaultRoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetDefaultRoomToolStripMenuItem.Click
        SetDefaultRoom()
    End Sub

    Private Sub TeachersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TeachersToolStripMenuItem.Click
        Dim fEmp As New frmEmployee
        fEmp.ShowDialog()

    End Sub

    Private Sub SubjectsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubjectsToolStripMenuItem.Click
        Dim fSub As New frmSubject
        fSub.ShowDialog()
    End Sub

    Private Sub SectionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SectionsToolStripMenuItem.Click
        Dim fSec As New frmSection
        fSec.ShowDialog()
    End Sub

    Private Sub CSFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CSFToolStripMenuItem.Click
        Dim fcsf As New frmCSF
        fcsf.ShowDialog()
    End Sub

    Private Sub RoomsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RoomsToolStripMenuItem.Click
        Dim froom As New frmRoom
        froom.ShowDialog()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Form1_Load(Nothing, Nothing)
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        _colorMode = Not _colorMode
    End Sub

    Private Sub SelctBatchG1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelctBatchG1ToolStripMenuItem.Click
        _Batch = 1
        UpdateBatchRadio()
    End Sub

    Private Sub SelectBatchG2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectBatchG2ToolStripMenuItem.Click
        _Batch = 2
        UpdateBatchRadio()
    End Sub
    Private Sub UpdateBatchRadio()
        If _Batch = 0 Then BatchF.Checked = True
        If _Batch = 1 Then RadioButton1.Checked = True
        If _Batch = 2 Then RadioButton2.Checked = True
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
        _Batch = 0
        UpdateBatchRadio()

    End Sub

    Private Sub TimetablesPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimetablesPreviewToolStripMenuItem.Click
        Dim c As New TimetableView.Form1
        c.Show()
        Me.Refresh()
    End Sub




    Private Sub PublishToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PublishToolStripMenuItem.Click
        'Dim DBSchema As New sq
        Dim DBTableNames As New DataTable
        Dim ds As New DataSet
        'Dim DBTableRow As DataRow
        Dim SQLString As String
        SQLString = "Select * from V_Timetable_2013"
        ' Dim cmd As SqlCommand
        Dim DbConnection As New SqlConnection
        DbConnection.ConnectionString = My.Settings.eCollegeConnectionString
        Dim ad As New SqlDataAdapter
        ad.SelectCommand = New SqlCommand(SQLString, DbConnection)
        Dim dbc As SqlCommandBuilder
        dbc = New SqlCommandBuilder(ad)
        Try
            ad.Fill(ds)
        Catch eror As Exception
            MessageBox.Show(eror.Message)
        Finally
            dbc.Dispose()
            ad.Dispose()
            DbConnection.Close()
        End Try

        Try
            ds.WriteXml("D:\BackUp.xml")
            ds.WriteXmlSchema("D:\BackUp.xsd")
        Catch
        End Try

    End Sub

    Private Sub TestToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TestToolStripMenuItem.Click
        Dim f As New TimetableView.frmXMLBased
        f.Show()
    End Sub

    Private Sub ExportTeacherToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportTeacherToolStripMenuItem.Click
        Dim q1 As String = "SELECT distinct [name] FROM [TIMETABLE].[dbo].[Teacher]"
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(q1, cn)
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader
        Dim Str As String = """" & "Teacher" & """"
        While rd.Read
            Str += vbNewLine & """" & rd(0) & """"
        End While
        cn.Close()
        Dim w As StreamWriter
        w = File.CreateText("C:\Users\awasthi\fet-results\csv\gbu2014\gbu2014_teachers.csv")
        w.Write(Str)
        w.Flush()
        w.Close()

    End Sub

    Private Sub ExportSubjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportSubjectToolStripMenuItem.Click
        Dim q1 As String = "SELECT distinct [name] FROM [TIMETABLE].[dbo].[Subject]"
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(q1, cn)
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader
        Dim Str As String = """" & "Subject" & """"
        While rd.Read
            Str += vbNewLine & """" & rd(0) & """"
            End While
        cn.Close()
        Dim w As StreamWriter
        w = File.CreateText("C:\Users\awasthi\fet-results\csv\gbu2014\gbu2014_subjects.csv")
        w.Write(Str)
        w.Flush()
        w.Close()
    End Sub

    Private Sub ExportSectionsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportSectionsToolStripMenuItem.Click
        Dim q1 As String = "SELECT * FROM [Section]"
        ' MsgBox(q1)
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(q1, cn)
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader
        Dim Str As String = """Year"",""Number of Students per Year"",""Group"",""Number of Students per Group"",""Subgroup"",""Number of Students per Subgroup"""

        ' """Year","Number of Students per Year","Group","Number of Students per Group","Subgroup","Number of Students per Subgroup"""
        While rd.Read
            If rd(8) = 1 Then Str += vbNewLine & """" & rd(1).ToString.Trim & """," & """" & 60 & """," & """" & "" & """," & """" & "" & """," & """" & "" & """," & """" & "" & """"
            If rd(8) = 2 Then
                Str += vbNewLine & """" & rd(1).ToString.Trim & """," & """" & 60 & """," & """" & rd(1).ToString.Trim & "-G1" & """," & """" & 30 & """," & """" & "" & """," & """" & "" & """"
                Str += vbNewLine & """" & rd(1).ToString.Trim & """," & """" & 60 & """," & """" & rd(1).ToString.Trim & "-G2" & """," & """" & 30 & """," & """" & "" & """," & """" & "" & """"
            End If
        End While
        cn.Close()
        Dim w As StreamWriter
        w = File.CreateText("d:\Timetables2014\gbu2014_students.csv")
        w.Write(Str)
        w.Flush()
        w.Close()
    End Sub

    Private Sub ExportRoomsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportRoomsToolStripMenuItem.Click
        Dim q1 As String = "SELECT * FROM [TIMETABLE_JAN2014.MDF].[dbo].[M_Room]"
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(q1, cn)
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader
        Dim Str As String = """Room"",""Room Capacity"",""Building"""
        ' """Year","Number of Students per Year","Group","Number of Students per Group","Subgroup","Number of Students per Subgroup"""
        While rd.Read
            Str += vbNewLine & """" & rd(1).ToString.Trim & """," & """" & rd(4) & """," & """" & rd(7) & """"
        End While
        cn.Close()
        Dim w As StreamWriter
        w = File.CreateText("C:\Users\awasthi\fet-results\csv\gbu2014\gbu2014_rooms_and_buildings.csv")
        w.Write(Str)
        w.Flush()
        w.Close()
    End Sub

    Private Sub ExportCSFToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportCSFToolStripMenuItem.Click
        Dim q1 As String = "SELECT [Name],[Subject_Code],[Teacher_Name],* FROM [TIMETABLE_JAN2014.MDF].[dbo].[CSF_View]"
        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(q1, cn)
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader
        Dim Str As String = """Students Sets"",""Subject"",""Teachers"",""Activity Tags"",""Total Duration"",""Split Duration"",""Min Days"",""Weight"",""Consecutive"""
        '"""Students Sets"",""Subject"",""Teachers"",""Activity Tags"",""Total Duration"","Split Duration"",""Min Days"",""Weight"",""Consecutive"""
        ' """Year","Number of Students per Year","Group","Number of Students per Group","Subgroup","Number of Students per Subgroup"""
        While rd.Read
            Str += vbNewLine & """" & rd(0).ToString.Trim & """," & """" & rd(1).ToString.Trim & """," & """" & rd(2) & """," & """" & "Lecture" & """," & 3 & ",""""," & 1 & "," & ","
        End While
        cn.Close()
        Dim w As StreamWriter
        w = File.CreateText("C:\Users\awasthi\fet-results\csv\gbu2014\gbu2014_activities.csv")
        w.Write(Str)
        w.Flush()
        w.Close()
    End Sub



    Private Sub ImportNWToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ImportNWToolStripMenuItem.Click
        Dim fITT As New frmImportTimetable
        fITT.ShowDialog()
    End Sub

    Private Sub ExportActivitiesToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportActivitiesToolStripMenuItem.Click
        'Dim q1 As String = "SELECT [Name],[Subject_Code],[Teacher_Name],* FROM [TIMETABLE_JAN2014.MDF].[dbo].[CSF_View]"
        Dim q1 As String = "SELECT  [Name], [Subject_Code], [Teacher_Name], [Subject_Id], [L], [T], [P]  FROM [CSF_View]"
        'SELECT   Section_Id, Subject_Code, Faculty_Id, Subject_ID, CreationDate, IsRemoved, LA, TA, PA
        'FROM(CSF)

        cn.ConnectionString = My.Settings.eCollegeConnectionString
        cn.Open()
        Dim cmd As New SqlCommand(q1, cn)
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader
        Dim Str As String = """Students Sets"",""Subject"",""Teachers"",""Activity Tags"",""Total Duration"",""Split Duration"",""Min Days"",""Weight"",""Consecutive"""
        '"""Students Sets"",""Subject"",""Teachers"",""Activity Tags"",""Total Duration"","Split Duration"",""Min Days"",""Weight"",""Consecutive"""
        ' """Year","Number of Students per Year","Group","Number of Students per Group","Subgroup","Number of Students per Subgroup"""
        While rd.Read
            If rd(4) > 0 Then Str += vbNewLine & """" & rd(0).ToString.Trim & """," & """" & rd(1).ToString.Trim & """," & """" & rd(2) & """," & """" & "Lecture" & """," & rd(4) & ",""""," & 1 & "," & ","
            If rd(5) > 0 Then
                If rd(5) = 1 Then Str += vbNewLine & """" & rd(0).ToString.Trim & """," & """" & rd(1).ToString.Trim & """," & """" & rd(2) & """," & """" & "Tutorial" & """," & rd(5) & ",""""," & 1 & "," & ","
                If rd(5) = 2 Then
                    Str += vbNewLine & """" & rd(0).ToString.Trim & "-G1" & """," & """" & rd(1).ToString.Trim & """," & """" & rd(2) & """," & """" & "Tutorial" & """," & 1 & ",""""," & 1 & "," & ","
                    Str += vbNewLine & """" & rd(0).ToString.Trim & "-G2" & """," & """" & rd(1).ToString.Trim & """," & """" & rd(2) & """," & """" & "Tutorial" & """," & 1 & ",""""," & 1 & "," & ","
                End If
            End If
            If rd(6) > 0 Then Str += vbNewLine & """" & rd(0).ToString.Trim & """," & """" & rd(1).ToString.Trim & """," & """" & rd(2) & """," & """" & "Lab" & """," & rd(6) & ",""2""," & 1 & "," & ","
        End While
        cn.Close()
        Dim w As StreamWriter
        w = File.CreateText("d:\Timetables2014\gbu2014_activities.csv")
        w.Write(Str)
        w.Flush()
        w.Close()
    End Sub

    Private Sub CourseStructureToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CourseStructureToolStripMenuItem.Click
        frmCS.ShowDialog()
    End Sub

    Private Sub CheckRoomSchool()
        If rSOBT.Checked Then _school = "SOBT"
        If rSOE.Checked Then _school = "SOE"
        If rICT.Checked Then _school = "SOICT"
        If rSOM.Checked Then _school = "SOM"
        If rSOVSAS.Checked Then _school = "SOVSAS"
        If rAll.Checked Then _school = "NONE"
    End Sub

    Private Sub DataGridView1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
        'MsgBox(DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value)
        LoadRoomTT(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        _room = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value
        ToolStripLabel1.Text = "Current Room selected: " & DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value
    End Sub

    
    Private Sub DeleteToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        '
        'Dim TTPeriod = TableLayoutPanel1.GetColumn(sender) + 1

        'Dim ts As New Timesheet
        'ts.DeleteTTSlot(TTDay, TTPeriod, TableLayoutPanel1, _section, _csf, _room, _Batch)
        'LoadTT(_section)
        'beforeDel = _currrow
        'CSFUpdate(cbSection.ComboBox.SelectedValue)
    End Sub

    Private Sub TableLayoutPanel1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseClick
        Dim TTDay = TableLayoutPanel1.GetRow(sender) + 1
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ContextMenuStrip1.Show()
            MsgBox(TTDay)
        End If
    End Sub

   
    
End Class
'Public Sub restore(ByVal dat As Date, Optional ByVal Encrypt As Boolean = False, Optional ByVal key As String = "")
'    frmMain.ActiveForm.Text = "Waiting"
'    Try
'        Shell("""..\\gzip.exe"" -drqf """ & Dest & "\Backup\" & dat.Month & "-" & dat.Day & "-" & dat.Year & """", , True)
'    Catch e As Exception
'        MessageBox.Show("No Restore folder on " & dat & " in your " & Dest & " Folder")
'        Exit Sub
'    End Try
'    Dim name As String
'    Dir = Dest & "\Backup\" & dat.Month & "-" & dat.Day & "-" & dat.Year
'    Call connection()
'    DBConnection.Open()
'    Dim DBSchema As New OleDb.OleDbSchemaGuid
'    Dim DBTableNames As New DataTable
'    DBTableNames = DBConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
'    Dim DBTableRow As DataRow
'    Dim SQLString As String
'    'DBDataSet.Clear()
'    DBNewDS.ReadXmlSchema(Dir & "/BackUp.xsd")
'    DBNewDS.ReadXml(Dir & "/BackUp.xml")
'    DBDataSet.Clear()
'    DBDataSet.Merge(DBNewDS, True)
'    For Each DBTableRow In DBTableNames.Rows
'        If DBTableRow.Item("TABLE_TYPE") = "TABLE" Then
'            name = CStr(DBTableRow.Item("TABLE_NAME"))
'            SQLString = "Select * from " & name
'            DBDataAdapter.SelectCommand = New OleDb.OleDbCommand(SQLString, DBConnection)
'            ' get data from the database and put them in memory
'            Try
'                Dim DBUpdateCommand As New OleDb.OleDbCommandBuilder(DBDataAdapter)
'                DBDataAdapter.Update(DBDataSet, name)
'            Catch eror As Exception
'                MessageBox.Show(eror.Message & ".source=" & eror.Source & "in table" & name)
'            Finally
'                DBDataAdapter.Dispose()
'                DBConnection.Close()
'            End Try
'        End If
'    Next
'    Try
'        Shell("""..\\gzip.exe"" -rqf """ & Dest & "\Backup\" & dat.Month & "-" & dat.Day & "-" & dat.Year & """", , True)
'    Catch e As Exception
'        MessageBox.Show("No Restore folder on " & dat & " in your " & Dest & " Folder")
'        Exit Sub
'    End Try
'    frmMain.ActiveForm.Text = "Alfa Restore"